<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // push方法的仿写
        Array.prototype.myPush = function () {
            for (let i = 0; i < arguments.length; i++) {
                this[this.length] = arguments[i];
            }
            return this.length
        }
        Array.prototype.myUnshift = function () {
            //unshift的实现需要两步
            //第一步将原数组中的所有元素向有移动(距离为添加的内容长度)
            //第二步将添加的内容依次放入之前按的空位中
            let num = arguments.length;
            for (let i = 0; i < num; i++) {
                this[i + num] = this[i];
            }
            for (let j = 0; j < num; j++) {
                this[j] = arguments[j];
            }
            return this.length
        }
        let arr = [1, 2, 3];
        // 测试
        arr.myUnshift(4, 5, 6)
        arr.push(7, 8, 9)
        console.log(arr)
        // 打印结果：[4,5,6,1,2,3,7,8,9]
    </script>
</body>

</html>